// ==UserScript==
// @name           WangWang Protocol Handler
// @namespace      com.xujiwei.iwangwang.userjs
// @include        http://*.taobao.com/*
// ==/UserScript==

/*!////////////////////////////////////////////////////////////////////////
//                                                                       //
//   Chrome WangWang Protocol Handler Extension                          //
//   Copyright (c) 2009-2010 Jiwei Xu <ohdarling88 at gmail d0t com>     //
//                                                                       //
/////////////////////////////////////////////////////////////////////////*/

(function() {
    var document;
    try {
        document = unsafeWindow.document;
    } catch(e) {
        try {
            document = window.document;
        } catch(e) { }
    }

    if (!document) {
        return;
    }

    /**
     * Check if the element is a valid wangwang chat element.
     * @param   {HTMLElement}   el      Usually the param is a A element
     * @return  {Boolean}               True if the element is a valid wangwang chat element
     */
    var isValidWWElement = function(el) {
        var elp = el.parentNode,
            className = elp.className;

        return el.tagName === "A" &&
               el.href.substr(0, 6) !== "aliim:" &&
               (className.indexOf("J_WangWang") > -1 || className.indexOf("ww:token") > -1);
    };


    /**
     * Parse a query string to an object.
     * @param   {String}    str     The query string to parse
     * @return  {Object}            Key-Value object of the query string
     */
    var parseQueryString = function(str) {
        var qarr = str.split("&"),
            qparam, ret = [];

        for (var i = 0; i < qarr.length; ++i) {
            qparam = qarr[i].split("=");
            ret[qparam[0]] = qparam[1];
        }

        return ret;
    };


    /**
     * Get chat parameters from element's property
     * @param   {HTMLElement}   el      Usually the param is a A element
     * @return  {Object}                The chat params object
     */
    var getChatParams = function(el) {
        var elp = el.parentNode,
            className = elp.className,
            toUid,
            itemId;

        if (className.indexOf("J_WangWang") > -1) {
            // New wangwang lighter method
            toUid = elp.getAttribute("data-nick");
            itemId = elp.getAttribute("data-item");

        } else if (className.indexOf("ww:token") > -1) {
            // Old wangwang lighter method
            params = parseQueryString(elp.getAttribute("ww:params"));
            toUid = params.nick;
            itemId = params.item;
        }

        return {
            toUid : toUid,
            itemId : itemId
        };
    };

    /**
     * Monitor the mousedown event to prevent wangwang chat element's onclick function
     */
    document.addEventListener("mousedown", function(e) {
        var el = e.target, cmd, params;
        if (isValidWWElement(el)) {
            params = getChatParams(el);
            try {
                params.toUid = decodeURIComponent(params.toUid);
            } catch(err) { }
            cmd = "aliim:sendmsg?touid=cntaobao" + encodeURIComponent(params.toUid || "");
            el.setAttribute("onclick", "");
            el.setAttribute("target", "_self");
            el.href = cmd;
        }
    }, false);
})();
